/**
 * Created by Administrator on 2017/3/2 0002.
 */
var Page8={
    create:function () {
    var page={};

        var t0,t1,t2,t3;
        var tw_whole,tw_t0,tw_t1,tw_t2,tw_t3,tw_click;
        page.ui=new PIXI.DisplayObjectContainer();

        var bg=new PIXI.Sprite(resource['assets/images/p8_bg.png'].texture);
        page.ui.addChild(bg);

        var t0=new PIXI.Sprite(resource['assets/images/p8_t0.png'].texture);
        t0.x=376;
        t0.y=640;
        t0.alpha=0;
        t0.anchor.set(0.5);
        page.ui.addChild(t0);

        var t1=new PIXI.Sprite(resource['assets/images/p8_t1.png'].texture);
        t1.x=370;
        t1.y=768;
        t1.alpha=0;
        t1.anchor.set(0.5);
        page.ui.addChild(t1);

        var t2=new PIXI.Sprite(resource['assets/images/p8_t2.png'].texture);
        t2.x=366;
        t2.y=881;
        t2.alpha=0;
        t2.anchor.set(0.5);
        page.ui.addChild(t2);
        t2.on('pointerdown',downHandler);
        t2.on('pointerup',upHandler);
        tw_t2=new TWEEN.Tween({scale:0.9,alpha:0.9})
            .to({scale:1,alpha:1},500)
            .onUpdate(function () {
                t2.scale.x=this.scale;
                t2.scale.y=this.scale;
                t2.alpha=this.alpha;
            })
            .repeat(Infinity)
            .yoyo(true)
            // .start();

        var t3=new PIXI.Sprite(resource['assets/images/p8_t3.png'].texture);
        t3.x=370;
        t3.y=982;
        t3.alpha=0;
        t3.anchor.set(0.5);
        page.ui.addChild(t3);



        page.In=function () {
            if(tw_whole)
            {
                tw_whole.stop();
                TWEEN.remove(tw_whole);
            }

            tw_t0=new TWEEN.Tween({scale:0,alpha:0})
                .to({scale:1,alpha:1},1000)
                .delay(3500+100)
                .onUpdate(function () {
                    t0.alpha=this.alpha;
                    t0.scale.x=this.scale;
                    t0.scale.y=this.scale;
                })
                .easing(TWEEN.Easing.Back.Out)
                .start();
            tw_t1=new TWEEN.Tween({scale:0,alpha:0})
                .to({scale:1,alpha:1},1000)
                .delay(3000+300)
                .onUpdate(function () {
                    t1.alpha=this.alpha;
                    t1.scale.x=this.scale;
                    t1.scale.y=this.scale;
                })
                .easing(TWEEN.Easing.Back.Out)
                .start();
            // tw_t2=new TWEEN.Tween({alpha:0})
            //     .to({alpha:1},1000)
            //     .delay(3500+500)
            //     .onUpdate(function () {
            //         t2.alpha=this.alpha;
            //     })
            //     .easing(TWEEN.Easing.Back.Out)
            //     .start();
            setTimeout(function () {
                t2.interactive=true;
                tw_t2.start();
            },3500+500)

            tw_t3=new TWEEN.Tween({scale:0,alpha:0})
                .to({scale:1,alpha:1},1000)
                .delay(3000+700)
                .onUpdate(function () {
                    t3.alpha=this.alpha;
                    t3.scale.x=this.scale;
                    t3.scale.y=this.scale;
                })
                .easing(TWEEN.Easing.Back.Out)
                .start();
        }
        page.Out=function () {
            if(tw_t0)
            {
                tw_t0.stop();
            }
            if(tw_t1)
            {
                tw_t1.stop();
            }


            tw_whole=new TWEEN.Tween({alpha:1})
                .to({alpha:0},400)
                .onUpdate(function () {
                    page.ui.alpha=this.alpha;
                })
                .onComplete(function () {
                    page.ui.alpha=1;
                    t0.alpha=0;
                    t1.alpha=0;
                })
                .easing(TWEEN.Easing.Cubic.InOut)
                .start();
        }

        var downPT={x:0,y:0};
        function upHandler(e) {
            var dis=(e.data.global.x-downPT.x)*(e.data.global.x-downPT.x)-(e.data.global.y-downPT.y)*(e.data.global.y-downPT.y)
            if(dis<25)
            {
                console.log('click');
                t2.interactive=false;
                pageOut();
            }
        }
        function downHandler(e) {
            downPT.x=e.data.global.x;
            downPT.y=e.data.global.y;
        }
        function pageOut()
        {
            if(tw_click)
            {
                tw_click.stop();
            }
            var event = new CustomEvent("Game_PageOut", {detail:8});
            var event = new CustomEvent("Game_PageIn", {detail:9});
            document.dispatchEvent(event);
            new TWEEN.Tween({scale:1,x:0,y:0,alpha:1})
                .to({scale:10,x:-1562,y:-1156,alpha:0},1500)
                .onUpdate(function () {
                    page.ui.alpha=this.alpha;
                })
                .easing(TWEEN.Easing.Cubic.InOut)
                .start();
        }

    return page;
    }
}